home *** CD-ROM | disk | FTP | other *** search
/ Light ROM 1 / LIGHT-ROM 1 (Amiga Library Services)(1994).iso / ffdisks / d894.lha / Resize / resize.c < prev   
C/C++ Source or Header  |  1993-06-09  |  7KB  |  253 lines

  1. /*
  2.  *  New, improved Resize-Tool by Bernd Raschke
  3.  */
  4.  
  5. #include <exec/types.h>
  6. #include <exec/memory.h>
  7. #include <intuition/intuition.h>
  8. #include <intuition/screens.h>
  9. #include <graphics/gfxbase.h>
  10. #include <libraries/dos.h>
  11. #include <libraries/dosextens.h>
  12. #include <devices/conunit.h>
  13.  
  14. #include <clib/graphics_protos.h>
  15. #include <clib/intuition_protos.h>
  16. #include <clib/exec_protos.h>
  17. #include <clib/dos_protos.h>
  18.  
  19. #include <stdlib.h>
  20. #include <stdio.h>
  21. #include <string.h>
  22. #include <ctype.h>
  23.  
  24. /* Used for checking version */
  25. #define VERSION     "1"
  26. #define REVISION    "3"
  27.  
  28. struct Library *DosBase;
  29. struct GfxBase *GfxBase;
  30. struct IntuitionBase *IntuitionBase;
  31.  
  32. /* Globals initialized by findWindow() */
  33. struct Window *conWindow;
  34. struct Screen *conScreen;
  35. struct RastPort *conRP;
  36. struct ConUnit *conUnit;
  37. struct TextFont *scTFont;
  38. BYTE bl, bt, br, bb;
  39. WORD cwid_p, chgt_p, cwid_c, chgt_c, ciwid_p, cihgt_p;
  40. WORD nwid_c, nhgt_c, nwid_p, nhgt_p, fwid_p, fhgt_p;
  41. WORD scWidth, scHeight, ctop_p, clft_p, ntop_p, nlft_p;
  42.  
  43. UBYTE Version[] = "$VER: resize " VERSION "." REVISION " ("__DATE__")";
  44.  
  45. void cleanexit(char *);
  46. void cleanup(void);
  47. void parseArgs(char *);
  48. LONG findWindow(void);
  49. void examWindow(void);
  50. LONG sendpkt(struct MsgPort *,LONG , LONG *,LONG );
  51. void examfont(void);
  52. void main(int, char **);
  53.  
  54. void cleanexit(char *s)
  55. {
  56.     if(*s)
  57.     printf(s);    /* Print error */
  58.     cleanup();
  59.     exit(0);
  60. }
  61.  
  62. void cleanup(void)
  63. {
  64.     if(DosBase)
  65.     CloseLibrary(DosBase);
  66.     if(GfxBase)
  67.     CloseLibrary((struct Library *) GfxBase);
  68.     if(IntuitionBase)
  69.     CloseLibrary((struct Library *) IntuitionBase);
  70. }
  71.  
  72. /*
  73.     Analyse the string we got from GEOM. I tried to make it similar to the
  74.     '-geometry' option of the X Window System... (well, sort of :-)
  75.     The code is a bit weird, but it works ok.
  76.  
  77.     "XSize"x"YSize"V"XOffset"V"YOffset", where size is measured in characters
  78.     and offset in pixels. V stands for '+' (left/top) and '-' (right/bottom)
  79. */
  80.  
  81. void parseArgs(char *v)
  82. {
  83.     int i=0, j=0;
  84.     char dx[10], dy[10], x[10], y[10], sx, sy;
  85.     WORD xi, yi;
  86.  
  87. /*  Scan for the width. We'll only take numbers */
  88.     while((isdigit(v[i]) != 0) && (v[i] != '\0'))
  89.     dx[j++] = v[i++];
  90.     dx[j]= 0;
  91.  
  92. /*  Is there a x? Do we have a height? Can birds fly? */
  93.     j=0;
  94.     if(v[i] == 'x')
  95.     {
  96.     i++;
  97.     while((isdigit(v[i]) != 0) && (v[i] != '\0'))
  98.         dy[j++] = v[i++];
  99.     dy[j]= 0;
  100.     }
  101.  
  102. /*  Now the x-offset. sx will be the sign */
  103.     j=0;
  104.     sx= v[i++];
  105.     while((isdigit(v[i]) != 0) && (v[i] != '\0'))
  106.     x[j++] = v[i++];
  107.     x[j]= 0;
  108.  
  109. /*  Now the y-offset. sy will be the sign */
  110.     j=0;
  111.     sy= v[i++];
  112.     while((isdigit(v[i]) != 0) && (v[i] != '\0'))
  113.     y[j++] = v[i++];
  114.  
  115. /*  Calculate the new (width|height) in characters and offsets */
  116.     if((nwid_c= (WORD) atoi(dx)) == 0)
  117.     nwid_c= cwid_c;
  118.     if((nhgt_c= (WORD) atoi(dy)) == 0)
  119.     nhgt_c= chgt_c;
  120.     xi= (WORD) atoi(x);
  121.     yi= (WORD) atoi(y);
  122.  
  123. /*  new width in pixels is new width in chars * font width in pixels +
  124.     +borderleft + borderright (equivalent for height) */
  125.     nwid_p= nwid_c * fwid_p +bl +br;
  126.     nhgt_p= nhgt_c * fhgt_p +bt +bb;
  127.  
  128. /*  Now handle the difference between '+' and '-' */
  129.     if(sx == '+')
  130.     nlft_p= xi;
  131.     else if(sx == '-')
  132.     nlft_p= scWidth -nwid_p -xi;
  133.     else
  134.     nlft_p= clft_p;
  135.  
  136.     if(sy == '+')
  137.     ntop_p= yi;
  138.     else if(sy == '-')
  139.     ntop_p= scHeight -nhgt_p -yi;
  140.     else
  141.     ntop_p= ctop_p;
  142.  
  143.     return;
  144. }
  145.  
  146. /*  This is the last remaining code from the example:
  147.     ConPackets.c -    C. Scheppner, A. Finkel, P. Lindsay  CBM
  148. */
  149. LONG findWindow(void) /* inits conWindow and conUnit (global vars) */
  150. {
  151.     struct InfoData *id;
  152.     struct MsgPort  *conid;
  153.     struct Process  *me;
  154.     LONG myarg, res1;
  155.  
  156.    /* Alloc to insure longword alignment */
  157.     id = (struct InfoData *)AllocMem(sizeof(struct InfoData),
  158.                        MEMF_PUBLIC|MEMF_CLEAR);
  159.     if(! id) return(0);
  160.     me = (struct Process *) FindTask(NULL);
  161.     conid = (struct MsgPort *) me->pr_ConsoleTask;
  162.  
  163.     myarg= ((ULONG)id) >> 2;
  164.     res1 = (LONG)DoPkt(conid,ACTION_DISK_INFO, myarg, 0L, 0L, 0L, 0L);
  165.     conWindow = (struct Window *)id->id_VolumeNode;
  166.     conScreen= conWindow->WScreen;
  167.     conUnit = (struct ConUnit *) ((struct IOStdReq *)id->id_InUse)->io_Unit;
  168.     FreeMem(id,sizeof(struct InfoData));
  169.     return(res1);
  170. }
  171. /* This is the End */
  172.  
  173. void examWindow(void)
  174. {
  175.     bl= conWindow->BorderLeft;
  176.     bt= conWindow->BorderTop;
  177.     br= conWindow->BorderRight;
  178.     bb= conWindow->BorderBottom;
  179.     conRP= conWindow->RPort;
  180.     cwid_p= conWindow->Width;
  181.     chgt_p= conWindow->Height;
  182.     ciwid_p= cwid_p -bl -br;
  183.     cihgt_p= chgt_p -bt -bb;
  184.     scWidth= conScreen->Width;
  185.     scHeight= conScreen->Height;
  186.     clft_p= conWindow->LeftEdge;
  187.     ctop_p= conWindow->TopEdge;
  188.  
  189.     return;
  190. }
  191. void examFont(void)
  192. {
  193.     scTFont= conRP->Font;
  194.     fwid_p= scTFont->tf_XSize;
  195.     fhgt_p= scTFont->tf_YSize;
  196.  
  197.     cwid_c= ciwid_p / fwid_p;
  198.     chgt_c= cihgt_p / fhgt_p;
  199.     return;
  200. }
  201.  
  202. void main(int argc, char **argv)
  203. {
  204.     LONG  infile;
  205.  
  206.     if(!(DosBase=(OpenLibrary((UBYTE *) "dos.library",37L))))
  207.     cleanexit("\nCouldn't open dos.library. This program needs at least Kickstart2.04\n");
  208.  
  209.     if(!(GfxBase=(struct GfxBase *)OpenLibrary((UBYTE *) "graphics.library", 37L)))
  210.     cleanexit("\nCouldn't open graphics.library. This program needs at least Kickstart2.04\n");
  211.  
  212.     if(!(IntuitionBase=(struct IntuitionBase *)OpenLibrary((UBYTE *) "intuition.library", 37L)))
  213.     cleanexit("\nCouldn't open intuition.library. This program needs at least Kickstart2.04\n");
  214.  
  215.     if((infile = Input()) <= 0)
  216.     cleanexit("\nNo stdin ... Did you RUN this ???\n");
  217.  
  218.     if (! findWindow()) cleanexit("\nNot enough free memory\n");
  219.  
  220.     examWindow();
  221.     examFont();
  222.  
  223.     if(argc == 1)
  224.     {
  225.     printf("console dimensions: %dx%d+%d+%d(%dx%d)\n", cwid_c, chgt_c,
  226.         conWindow->LeftEdge, conWindow->TopEdge,
  227.         conWindow->Width, conWindow->Height);
  228.     } else if((argc != 2) || (strcmp(argv[1], "+help") == 0) ||
  229.     (strcmp(argv[1], "-?") == 0) ||
  230.     (strcmp(argv[1], "?") == 0))
  231.     {
  232.     puts("resize " VERSION "." REVISION " by Bernd Raschke,(" __DATE__ ")");
  233.     puts("Usage: resize [WIIDTH][xHEIGHT]][(+|-)XOFFSET[(+|-)YOFFSET]]");
  234.     puts("\tWIDTH and HEIGHT are measured in characters, XOFFSET and YOFFSET in");
  235.     puts("\tpixels from the border of the screen. '+' means left or down and");
  236.     puts("\t'-' means right and up. (Similar to the X Window System conventions.)\n");
  237.     puts("Valid examples:");
  238.     puts("  resize\n\treports current size and position");
  239.     puts("  resize 80x24+0+0\n\tsets size to 80 columns and 24 rows, in the upper left");
  240.     puts("  resize x-100-0\n\tkeeps the same size and moves window 100 pixel from right");
  241.     puts("\tat the bottom of the screen.");
  242.     } else
  243.     {
  244.     parseArgs(argv[1]);
  245.  
  246.     ChangeWindowBox(conWindow, nlft_p, ntop_p,
  247.         nwid_p, nhgt_p);
  248.     }
  249.     cleanup();
  250.     exit(0);
  251. }
  252.  
  253.